package com.app.shop.activitys;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.shop.R;
import com.app.shop.adapters.ZHListAdapter;
import com.app.shop.beans.GoodsBean;
import com.app.shop.beans.ZHBean;
import com.app.library.base.BaseActivity;
import com.app.library.beans.EventMessage;
import com.app.library.utils.UserManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ZHListActivity extends BaseActivity implements View.OnClickListener {


    private TextView tvTitle;
    private  TextView tv_sum;
    private RecyclerView recy;
    private ImageView imgvreturn;
    List<ZHBean> list = new ArrayList<>();

    int from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        tv_sum = (TextView) findViewById(R.id.tv_sum);
        recy = (RecyclerView) findViewById(R.id.recy);
        imgvreturn = (ImageView) findViewById(R.id.imgv_return);
        imgvreturn.setOnClickListener(this::onClick);
        Bundle bundle = getIntent().getExtras();
        from = bundle.getInt("from", 0);
        tv_sum.setVisibility(View.GONE);
        if (from == 1) {
            tvTitle.setText("置换申请");
        } else if (from == 2) {
            tvTitle.setText("被置换申请");
        }
        initData();
    }

    private void initData() {
        list.clear();
        List<ZHBean> list_all = DataSupport.findAll(ZHBean.class);
        for (int i = 0; i < list_all.size(); i++) {
            if (from == 1) {
                if (list_all.get(i).zh_goods_user_id.equals(UserManager.getUserId(this))) {
                    list.add(list_all.get(i));
                }
            } else if (from == 2) {
                if (list_all.get(i).user_id.equals(UserManager.getUserId(this))) {
                    list.add(list_all.get(i));
                }
            }
        }

        Collections.reverse(list);
        ZHListAdapter adapter = new ZHListAdapter(R.layout.item_zh_list, list, from);
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.view_list_empty, null));
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ContentValues values = new ContentValues();
                if (view.getId() == R.id.tv_left) {
                    values.put("zhihuan", 3);
                    list.get(position).zhihuan = 3;
                } else if (view.getId() == R.id.tv_right) {
                    values.put("zhihuan", 2);
                    list.get(position).zhihuan = 2;
                }

                DataSupport.updateAll(ZHBean.class, values, "z_id=?", list.get(position).z_id);
                DataSupport.updateAll(GoodsBean.class, values, "goods_id=?", list.get(position).goods_id);
                adapter.notifyDataSetChanged();
            }
        });
    }




    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            initData();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
        }
    }
}
